package com.example.rpc_core;

import com.example.rpc_core.loadbalance.LoadBalancer;
import com.example.rpc_core.loadbalance.impl.ConsistentHashLoadBalancer;
import com.example.rpc_core.model.ServiceInfo;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 负载均衡测试
 */
public class test2 {

        final LoadBalancer loadBalancer = new ConsistentHashLoadBalancer();

        @Test
        public void select() {
            // 请求参数
            Map<String, Object> requestParams = new HashMap<>();
            requestParams.put("methodName", "apple");
            // 服务列表
            ServiceInfo serviceMetaInfo1 = new ServiceInfo();
            serviceMetaInfo1.setServiceName("myService");
            serviceMetaInfo1.setServiceVersion("1.0");
            serviceMetaInfo1.setServiceHost("localhost");
            serviceMetaInfo1.setServicePort(1234);
            ServiceInfo serviceMetaInfo2 = new ServiceInfo();
            serviceMetaInfo2.setServiceName("myService");
            serviceMetaInfo2.setServiceVersion("1.0");
            serviceMetaInfo2.setServiceHost("yupi.icu");
            serviceMetaInfo2.setServicePort(80);
            List<ServiceInfo> serviceMetaInfoList = Arrays.asList(serviceMetaInfo1, serviceMetaInfo2);
            // 连续调用 3 次
            ServiceInfo serviceMetaInfo = loadBalancer.select(requestParams, serviceMetaInfoList);
            System.out.println(serviceMetaInfo);
            Assert.assertNotNull(serviceMetaInfo);
            serviceMetaInfo = loadBalancer.select(requestParams, serviceMetaInfoList);
            System.out.println(serviceMetaInfo);
            Assert.assertNotNull(serviceMetaInfo);
            serviceMetaInfo = loadBalancer.select(requestParams, serviceMetaInfoList);
            System.out.println(serviceMetaInfo);
            Assert.assertNotNull(serviceMetaInfo);
        }


}
